import json


class message_model():
    """
    所有消息模型
    """

    def __init__(self):
        self.msg = {
            '_from_id':'',  # 消息发送人ID，字符串格式。值为system的时候为系统消息
            '_from_name':"",
            'to_id':0,      # 消息发送对象
            'to_name':"",
            'key':"",   #
            'txt':"",
            'zdy':{},
            'send_type':1   # 消息发送对象类型  1 群聊  2 单聊
        }
        self.__get_sys_name()
        self.__get_msg_key()

    def __get_sys_name(self):
        self.sys = {
            'system':"系统通知"
        }

    def __get_msg_key(self):
        """获取所有消息类型"""
        self.all_key={
            'SendTxt':"发送文本消息",
            'SendImage':"发送图片消息",
            'SendFile': "发送文件",
            'SendVideo': "发送视频",
            'SendQues':"发送习题",
            'AddFriend': "添加好友",
            'DelFriend': "删除好友",
            'CrateGroup': "创建群",
            'DelGroup': "解散群",
            'JoinGroup': "加入群",
            'RemoveGroup':"移除群"
        }

    def sys_msg_model(self,**kwargs):
        """
        """
        res = self.__check_ks(True,**kwargs)
        if res:
            return res
        self.msg.update(dict(
            _from_id='system',
            _from_name=self.sys['system'],
            key=kwargs['key'],
            txt=kwargs['txt'],
            to_id=kwargs['to_id'],
            to_name=kwargs['to_name']
        ))
        return self.msg

    def msg_model(self,**kwargs):
        """
        普通消息
        """
        res = self.__check_ks(False, **kwargs)
        if res:
            return res
        self.msg.update(dict(
            _from_id=kwargs['_from_id'],
            _from_name=kwargs['_from_name'],
            key=kwargs['key'],
            txt=kwargs['txt'],
            to_id=kwargs['to_id'],
            to_name=kwargs['to_name'],
            zdy=kwargs.get('zdy',{}),
            send_type=kwargs.get('send_type',1)
        ))
        return self.msg

    def __check_ks(self,is_sys,**kwargs):
        """
        检查参数完整性
        """
        fields = ['key','to_id','to_name','txt']
        if not is_sys:
            fields.extend(['_from_id','_from_name'])
        ks_keys = list(kwargs.keys())
        for i in fields:
            if i not in ks_keys:
                return f'缺少参数：{i}'
        key = kwargs.get('key','')
        if key not in list(self.all_key.keys()):
            return '消息类型未定义'
        return ''

    def check_user_msg(self,obj):
        """
        检查用户发送的消息是否合规
        """
        if not isinstance(obj,dict):
            return '消息类型需为对象类型'
        obj_keys = list(obj.keys())
        for i in list(self.msg.keys()):
            if i not in obj_keys:
                return f'缺少参数：{i}'
        return ''

if __name__ == '__main__':
    d = message_model()
    """
    
    """
    msg = {'_from_id': 'system', '_from_name': '系统通知', 'to_id': 1, 'to_name': 'AAA', 'key': 'SendTxt', 'txt': '你好', 'zdy': {}}
    sys_msg = d.sys_msg_model(to_id=1,to_name='AAA',key='SendTxt',txt='你好')
    print(d.check_user_msg(msg))
